package com.changgou.seckill.service.impl;

import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SecKillGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SecKillGoodsServiceImpl implements SecKillGoodsService {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 更新商品秒杀对应的数据——价格、数量、限购数量、排序
     * @param seckillGoods
     */
    @Override
    public void updateGoods(SeckillGoods seckillGoods) {
        seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
    }

    /**
     * 分页查询活动商品
     * @param id
     * @return
     */
    @Override
    public Page<SeckillGoods> findById(int id ,int current,int pageSize) {
        PageHelper.startPage(current, pageSize);
        Page<SeckillGoods> pageById = seckillGoodsMapper.findPageById(id);
        return pageById;
    }

    /**
     * 分页条件查询
     * @param current
     * @param pageSize
     * @param searchMap
     * @return
     */
    @Override
    public Page<SeckillGoods> findAllSeckillGoods(int current , int pageSize , Map<String,String > searchMap) {
        PageHelper.startPage(current,pageSize);
        Example example = new Example(SeckillGoods.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
            criteria.andLike("title","%"+searchMap.get("title")+"%");
        }
        if (searchMap.get("sn")!=null && !"".equals(searchMap.get("sn"))){
            criteria.andLike("sn","%"+searchMap.get("sn")+"%");
        }
        Page<SeckillGoods> seckillGoods = (Page<SeckillGoods>) seckillGoodsMapper.selectByExample(example);
        return seckillGoods;
    }


}
